package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.BookedBean;
import com.store.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/3/11.
 * 尾货数据适配器
 */
public class TailCargoAdapter extends BaseAdapter {
    private Context context;
    private List<BookedBean> list = new ArrayList<>();
 //   WeakHashMap<Integer, View> map = new WeakHashMap<Integer, View>();
    public TailCargoAdapter(Context context,List<BookedBean> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final  int position, View convertView, ViewGroup parent) {
        //convertView = map.get(position);
        ViewHolder viewHolder = null;
        BookedBean item = list.get(position);
        if(convertView == null)
       {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.tail_cargo_item, null);
            viewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tvBanlanceCount = (TextView) convertView.findViewById(R.id.tv_banlance_count);
            viewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tvOldPrice = (TextView) convertView.findViewById(R.id.tv_old_price);
            viewHolder.btGo = (TextView) convertView.findViewById(R.id.bt_go);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
            viewHolder.tvPayWay = (TextView) convertView.findViewById(R.id.tvPayWay);
            convertView.setTag(viewHolder);			//表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder)convertView.getTag();//通过getTag的方法将数据取出来
        }
        viewHolder.tvName.setText(item.getTitle());
        viewHolder.tvOldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        String s_sale_qty=item.getSale_qty();
        int i_sale_qty=0;
        try{
            i_sale_qty=Integer.parseInt(s_sale_qty);
            if(i_sale_qty<0){
                i_sale_qty=0;
            }
        }catch (Exception e){

        }
        viewHolder.tvBanlanceCount.setText("剩余库存：" + i_sale_qty+item.getGoods_unit());
        viewHolder.tvPrice.setText(item.getDiscount_price() + "");
        viewHolder.tvOldPrice.setText("¥" + item.getMarket_price() + "");
         String url="";
        if(!TextUtils.isEmpty(item.getPic_info())){
            try {
                JSONArray jsonArray = new JSONArray(item.getPic_info());
                JSONObject jsonObject=jsonArray.getJSONObject(0);
                String s_id=jsonObject.getString("path_id");
                Map<String,String> map = Utils.getMapForJson(item.getPic_info_url());
                url=map.get(s_id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
        if(!TextUtils.isEmpty(item.getPic_info_url())&&!("{}".equals(item.getPic_info_url()))){
            Map map = Utils.getMapForJson(item.getPic_info_url());
            Collection<String> co=map.values();
            url  = co.iterator().next();
        }
        }
        viewHolder.iv.setBackgroundResource(R.drawable.bg4);
        ImageLoader.getInstance().displayImage(url,viewHolder.iv,options);
        String payment_way=item.getPayment_way();
        if(payment_way.equals("online")){
            viewHolder.tvPayWay.setText("在线支付");
            viewHolder.btGo.setText("立即抢购");
        }else{
            viewHolder.tvPayWay.setText("货到付款");
            viewHolder.btGo.setText("立即抢订");
        }
        viewHolder.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(list.get(position));
            }
        });
 //       map.put(position, convertView);
        return convertView;
    }
    /**
     * 定义一个内部类
     * 声明相应的控件引用
     *
     */
    public class ViewHolder {
        private TextView tvName;         //商品名称
        private TextView tvBanlanceCount;//库存
        private TextView btGo;            //立即抢购
        private TextView tvPrice;       //当前价格
        private TextView tvOldPrice;    //过去价格
        private ImageView iv;           //商品图片
        private TextView tvPayWay;      //配送方式

    }
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.bg4)
            .showImageOnFail(R.drawable.bg4)
            .showStubImage(R.drawable.bg4)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
